<?php

class ModuleSnsArmorgame extends ModuleSns
{
    /**
     * @see ModuleSns::__construct()
     */
    public function __construct($userId, $platform, $configs)
    {
        parent::__construct($userId, $platform, $configs);
    }

    /**
     * @see ModuleSns::createJsEmbededCode()
     */
    public function createJsEmbeddedCode()
    {
        return '';
    }

    /**
     * @see ModuleSns::initializeOnIndex()
     */
    public function initializeOnIndex()
    {
        return array($this->userId, $this->platform);
    }

    /**
     * @see ModuleSns::getPfToken()
     */
    public function getPfToken()
    {
        return SzParam::getReqParam('auth_token');;
    }

    /**
     * @see ModuleSns::getPfSignature()
     */
    public function getPfSignature()
    {
        return null;
    }

    /**
     * @see ModuleSns::getFriends()
     */
    public function getFriends($userId)
    {
        return array();
    }

    /**
     * @see ModuleSns::getPfFriends()
     */
    protected function getPfFriends($userId)
    {
        return array();
    }

    /**
     * @see ModuleSns::preparePfApiEnv()
     */
    protected function preparePfApiEnv()
    {
    }

    /**
     * @see ModuleSns::checkResult()
     */
    protected function checkResult($result)
    {
    }

    /**
     * 获取用户平台信息
     *
     * @param string $platformId
     * @param string $pfToken
     * * @return array
     * <pre>
     *  SUCCEED:
     *  {
     *      version: 1,
     *      code: 200,
     *      message: "OK",
     *      payload:
     *      {
     *          uid: "111599297335108c0dbb0c94d205940a",
     *          username: "Shinezone",
     *          avatar: "http://armatars.armorgames.com/armatar_10_50.50_c.png",
     *          avatar_med: "http://armatars.armorgames.com/armatar_10_100.100_c.png",
     *          avatar_lg: "http://armatars.armorgames.com/armatar_10_200.200_c.png"
     *          birthday: "0000-00-00",
     *          gender: "",
     *          created_on: 1399440132,
     *          plays_game: null,
     *          level: 0,
     *          premium: false
     *      }
     * }
     *  FAILURE: token error
     *  {
     *      version: 1,
     *      code: 200,
     *      message: "Either the user is not logged in or the authentication token has expired",
     *      payload: null
     *  }
     *  FAILURE: api key error
     *  {
     *      version: 1,
     *      code: 401,
     *      message: "Unauthorized"
     *  }
     * </pre>
     */
    public function getUserInfo($platformId, $pfToken)
    {
        $url = 'https://services.armorgames.com/services/rest/v1/authenticate/user.json?user_id=' . $platformId;
        $url .= '&auth_token=' . $pfToken;
        $url .='&api_key=' . $this->configs['appToken']['value'];
        $result = json_decode(SzUtility::getHttpRequest($url), true);

        if (200 == $result['code']) {
            return $result['payload'];
        } else {
            return array();
        }
    }

    /**
     * verify user was login.
     *
     * @param string $platformId
     * @param string $pfToken
     * @throws SzException 30203
     * @return void
     */
    public function verifyLogin($platformId, $pfToken)
    {
        $userInfo = $this->getUserInfo($platformId, $pfToken);
        if (is_null($userInfo) || empty($userInfo)) {
            throw new SzException(30203, null, ModuleSnsConst::MODULE_NAME);
        }
    }
}